/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ohos_nweb/bridge/ark_web_print_document_adapter_adapter_impl.h"

#include "ohos_nweb/bridge/ark_web_print_write_result_callback_adapter_wrapper.h"
#include "ohos_nweb/bridge/ark_web_print_attributes_adapter_wrapper.h"

#include "base/bridge/ark_web_bridge_macros.h"

namespace OHOS::ArkWeb {

ArkWebPrintDocumentAdapterAdapterImpl::ArkWebPrintDocumentAdapterAdapterImpl(
    std::unique_ptr<OHOS::NWeb::NWebPrintDocumentAdapterAdapter> ref)
{
    ref_ = std::move(ref);
}

void ArkWebPrintDocumentAdapterAdapterImpl::OnStartLayoutWrite(const ArkWebString& jobId,
    ArkWebRefPtr<ArkWebPrintAttributesAdapter> oldAttrs, ArkWebRefPtr<ArkWebPrintAttributesAdapter> newAttrs,
    uint32_t fd, ArkWebRefPtr<ArkWebPrintWriteResultCallbackAdapter> callback)
{
    auto oldAttributes = std::make_shared<NWebPrintAttributesAdapterWrapper>(oldAttrs);
    auto newAttributes = std::make_shared<NWebPrintAttributesAdapterWrapper>(newAttrs);

    if (CHECK_REF_PTR_IS_NULL(callback)) {
        return ref_->OnStartLayoutWrite(ArkWebStringStructToClass(jobId), oldAttributes, newAttributes, fd, nullptr);
    }

    ref_->OnStartLayoutWrite(ArkWebStringStructToClass(jobId), oldAttributes, newAttributes, fd,
        std::make_shared<ArkWebPrintWriteResultCallbackAdapterWrapper>(callback));
}

void ArkWebPrintDocumentAdapterAdapterImpl::OnJobStateChanged(const ArkWebString& jobId, uint32_t state)
{
    ref_->OnJobStateChanged(ArkWebStringStructToClass(jobId), state);
}

} // namespace OHOS::ArkWeb